﻿Public Class ListSong1
#Region "加载数据"
    Private _Id As String = ""
    Public Overrides Sub LoadData()

        'A-2009-10-11
        Me.MyDataView1.BrowserData = True
        'A-2009-10-11

        Dim strSql_1 As String
        Dim strSql1_1 As String
        strSql_1 = "select ID,(Songer||'/'||Song||Memo) as TITLE from XSCSong  "
        strSql_1 &= " ORDER BY [index],Songer,OPTIME  DESC"
        strSql1_1 = "select count(*) as PageCount from XSCSong  Where 1=1  "
        Me.MyDataView1.SetRsSQLITE(strSql_1, strSql1_1, ct, 1)
    End Sub

    Private Sub cbo类型_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        LoadData()
    End Sub

#Region "更新明细"
    Public Overrides Function DoSelect(ByVal strId As String) As Boolean
        MyBase.DoSelect(strId)
        _Id = strId
        Return True
    End Function
#End Region

    Public Overrides Function DoUpdateCheck() As Boolean
        Return True
    End Function

    Public Overrides Function DoUpdateExist() As Boolean
        Return True
    End Function
    Public Overrides Function DoUpdate() As Boolean
        If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim filenams() As String = OpenFileDialog1.FileNames
            Dim tempFilename As String = ""
            Dim tempSpitResult() As String
            Dim tempExtension As String = ""
            For Each filename As String In filenams
                tempFilename = System.IO.Path.GetFileNameWithoutExtension(filename)
                tempExtension = System.IO.Path.GetExtension(filename).ToLower
                If tempExtension = ".mp3" OrElse tempExtension = ".wma" Then
                    tempSpitResult = tempFilename.Split(New String() {"-"}, StringSplitOptions.RemoveEmptyEntries)
                    If tempSpitResult.Length = 2 Then
                        If Not DoExist(tempSpitResult(1), tempSpitResult(0)) Then
                            DoInsert1(tempSpitResult(1), tempSpitResult(0))
                        End If
                    End If
                End If
            Next
            ShowMessageBox("导入成功")
        End If
        Return True
    End Function

    Public Function DoExist(ByVal strSong As String, ByVal strSonger As String) As Boolean
        DoExist = False
        Dim rs As ADODB.Recordset = Nothing
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine("SELECT Song FROM XSCSong  WHERE Song=? And Songer=?")

        Dim pa As ADODB.Parameter = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput)
        If strSong.Trim().ToUpper().Length > 0 Then
            pa.Size = strSong.Trim().ToUpper().Length
        Else
            pa.Size = 10
        End If
        pa.Value = strSong.Trim().ToUpper()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput)
        If strSonger.Trim().ToUpper().Length > 0 Then
            pa.Size = strSonger.Trim().ToUpper().Length
        Else
            pa.Size = 10
        End If
        pa.Value = strSonger.Trim().ToUpper()
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        rs = ct.ExcCommandAsRs(cmd)
        If rs Is Nothing Then Exit Function
        If rs.EOF = False Then
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
            rs = Nothing
            DoExist = True
            Exit Function
        End If
        rs.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
    End Function

    Public Function DoInsert1(ByVal strSong As String, ByVal strSonger As String) As Boolean
        DoInsert1 = False
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine(" Insert Into ")
        sb.AppendLine(" XSCSong ")
        sb.AppendLine(" (")
        sb.AppendLine(" Song ")
        sb.AppendLine(" ,[index]")
        sb.AppendLine(" ,Songer")
        sb.AppendLine(" ,USERNAME ")
        sb.AppendLine(" ,OPTIME ")
        sb.AppendLine(" ,AddTime ")
        sb.AppendLine(" ,OptUser ) ")
        sb.AppendLine(" Values ")
        sb.AppendLine(" ( ")
        sb.AppendLine(" ? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ) ")

        Dim pa As ADODB.Parameter = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 100
        pa.Value = CMainModule.CmnToStr1(strSong).Trim()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 10
        pa.Value = getPY(CMainModule.CmnToStr1(strSonger).Trim())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 100
        pa.Value = CMainModule.CmnToStr1(strSonger).Trim()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName)
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
        ct.ExcCommand(cmd)
    End Function

    Public Function getPYChar(ByVal SingleChar As String) As String
        getPYChar = ""
        Dim lChar As Long
        lChar = 65536 + Asc(SingleChar)
        If (lChar >= 45217 And lChar <= 45252) Then getPYChar = "A"
        If (lChar >= 45253 And lChar <= 45760) Then getPYChar = "B"
        If (lChar >= 47761 And lChar <= 46317) Then getPYChar = "C"
        If (lChar >= 46318 And lChar <= 46825) Then getPYChar = "D"
        If (lChar >= 46826 And lChar <= 47009) Then getPYChar = "E"
        If (lChar >= 47010 And lChar <= 47296) Then getPYChar = "F"
        If (lChar >= 47297 And lChar <= 47613) Then getPYChar = "G"
        If (lChar >= 47614 And lChar <= 48118) Then getPYChar = "H"
        If (lChar >= 48119 And lChar <= 49061) Then getPYChar = "J"
        If (lChar >= 49062 And lChar <= 49323) Then getPYChar = "K"
        If (lChar >= 49324 And lChar <= 49895) Then getPYChar = "L"
        If (lChar >= 49896 And lChar <= 50370) Then getPYChar = "M"
        If (lChar >= 50371 And lChar <= 50613) Then getPYChar = "N"
        If (lChar >= 50614 And lChar <= 50621) Then getPYChar = "O"
        If (lChar >= 50622 And lChar <= 50905) Then getPYChar = "P"
        If (lChar >= 50906 And lChar <= 51386) Then getPYChar = "Q"
        If (lChar >= 51387 And lChar <= 51445) Then getPYChar = "R"
        If (lChar >= 51446 And lChar <= 52217) Then getPYChar = "S"
        If (lChar >= 52218 And lChar <= 52697) Then getPYChar = "T"
        If (lChar >= 52698 And lChar <= 52979) Then getPYChar = "W"
        If (lChar >= 52980 And lChar <= 53640) Then getPYChar = "X"
        If (lChar >= 53689 And lChar <= 54480) Then getPYChar = "Y"
        If (lChar >= 54481 And lChar <= 52289) Then getPYChar = "Z"
        Return getPYChar
    End Function

    Public Function getPY(ByVal str As String) As String
        getPY = ""
        For i As Integer = 0 To Len(str) - 1
            getPY = getPY & getPYChar(Mid(str, i + 1, 1))
        Next
    End Function

#Region "重载删除事件"
    Public Overrides Function DoDelete() As Boolean
        Dim CMD As New ADODB.Command
        CMD.CommandText = "DELETE from XSCSong  where ID='" + _Id + "'"
        If ct.ExcCommand(CMD) Then
            ShowMessageBox("删除成功")
        Else
            ShowMessageBox("删除失败")
        End If
        LoadData()
        Return True
    End Function
#End Region

#Region "删除CHECK"
    Public Overrides Function DoDeleteCheck() As Boolean
        DoDeleteCheck = False
        If ShowMessageResult("是否删除本条记录") = Windows.Forms.DialogResult.No Then Exit Function
        If _Id = "" Then
            ShowMessageBox("没有选中任何备注")
            Exit Function
        End If
        DoDeleteCheck = True
    End Function
#End Region

#Region "重载插入"
    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        Dim frm As New XSCSongAdd
        frm.iType = Me.iType
        frm.strName = Me.strName
        frm.ShowDialog()
        If frm.bAdd Then
            LoadData()
        End If
        frm.Dispose()
        frm = Nothing
        GC.Collect()
        Return True
    End Function
#End Region

#Region "重载设置窗体函数 A-2010-09-02"
    Public Overrides Function 初始行数() As Integer
        Return MyDataView1.PageSize
    End Function

    Public Overrides Sub 设置窗体数据行数()
        MyBase.设置窗体数据行数()
        MyDataView1.PageSize = Get窗体行数(MyDataView1.PageSize)
    End Sub

    Public Overrides Function 是否显示设置窗体数据行数() As Boolean
        Return True
    End Function
#End Region

#End Region

    Private Sub MyDataView1_ItemDoubleClickRefreshData(ByVal strId As System.String, ByRef bState As System.Boolean) Handles MyDataView1.ItemDoubleClickRefreshData
        Using frm As XSCSongAdd = New XSCSongAdd
            frm.参数列表.Add("ID", strId)
            frm.模式 = Wys.Control.Vb.InsertFormBase.模式型.修正
            frm.iType = Me.iType
            frm.strName = Me.strName
            frm.ShowDialog()

            If frm.bAdd Then
                LoadData()
            End If
        End Using
    End Sub

    Private _歌曲列表 As New DataTable
    Private _歌词列表 As New DataTable
    Private Sub MyDataView1_EventPageComplete() Handles MyDataView1.EventPageComplete
        If _歌曲列表 Is Nothing Then Exit Sub
        If _歌曲列表.Columns.Count = 0 Then Exit Sub

        Dim strContent As String = ""
        Dim strContents() As String
        Dim bExist As Boolean = False
        Dim bExist1 As Boolean = False
        For Each dr As System.Windows.Forms.DataGridViewRow In Me.MyDataView1.GetAllRow
            bExist = False
            strContent = dr.Cells(1).Value.ToString
            strContent = System.Text.RegularExpressions.Regex.Replace(strContent, "\([\w\W]+?\)", "")
            strContents = strContent.Split(New String() {"/"}, StringSplitOptions.None)
            If strContents.Length = 2 Then
                Dim drs() As DataRow = _歌曲列表.Select("作者='" + strContents(0).Trim + "' AND 歌曲名='" + strContents(1).Trim + "'")
                If drs.Length > 0 Then
                    bExist = True
                End If

                drs = _歌词列表.Select("作者='" + strContents(0).Trim + "' AND 歌曲名='" + strContents(1).Trim + "'")
                If drs.Length > 0 Then
                    bExist1 = True
                End If

            End If

            If dr.Cells(1).Value.ToString.Contains("本地不存在") OrElse dr.Cells(1).Value.ToString.Contains("歌词不存在") Then
                Continue For
            End If
            If Not bExist AndAlso Not bExist1 Then
                dr.Cells(1).Value = dr.Cells(1).Value + "-(本地不存在/歌词不存在)"
            ElseIf Not bExist AndAlso bExist1 Then
                dr.Cells(1).Value = dr.Cells(1).Value + "-(本地不存在)"
            ElseIf bExist AndAlso Not bExist1 Then
                dr.Cells(1).Value = dr.Cells(1).Value + "-(歌词不存在)"
            End If
        Next
    End Sub

    Private Sub ListSong1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Overrides Sub Init()
        MyBase.Init()
        _歌曲列表.Columns.Add("作者")
        _歌曲列表.Columns.Add("歌曲名")
        _歌曲列表.Columns.Add("歌曲路径")

        _歌词列表.Columns.Add("作者")
        _歌词列表.Columns.Add("歌曲名")

        Dim dif As New IO.DirectoryInfo(IO.Path.Combine(Me.GetCurrentDirectory(), "Song"))
        Try
            If dif IsNot Nothing Then
                For Each fif As IO.FileInfo In dif.GetFiles("*.*")
                    Dim content() As String = IO.Path.GetFileNameWithoutExtension(fif.Name).Split(New String() {"-"}, StringSplitOptions.None)
                    If content.Length = 2 Then
                        _歌曲列表.Rows.Add(New String() {content(0).Trim(), content(1).Trim(), dif.FullName})
                    End If
                Next
            End If
        Catch ex As Exception

        End Try

        dif = New IO.DirectoryInfo(IO.Path.Combine(Me.GetCurrentDirectory(), "Lyrics"))
        Try
            If dif IsNot Nothing Then
                For Each fif As IO.FileInfo In dif.GetFiles("*.lrc")
                    Dim content() As String = IO.Path.GetFileNameWithoutExtension(fif.Name).Split(New String() {"-"}, StringSplitOptions.None)
                    If content.Length = 2 Then
                        _歌词列表.Rows.Add(New String() {content(0).Trim(), content(1).Trim()})
                    End If
                Next
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class
